/*
 * Copyright 2011 SWM Services GmbH.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package de.swm.commons.mobile.client.widgets;

import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.uibinder.client.UiConstructor;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;

import de.swm.commons.mobile.client.SWMMobile;



/**
 * A ListItem with an Image.
 * 
 * <pre>
 * new DecoratedListItem(resources.gear3(), &quot;Command Panel&quot;, &quot;Sub Label &quot;);
 * </pre>
 * 
 */
public class DecoratedListItem extends ListItem {

	private Image img;
	private Label titleLabel;
	private Label subtitleLabel;



	/**
	 * Default constructor.
	 */
	public DecoratedListItem() {
		HorizontalPanel panel = new HorizontalPanel();
		img = new Image();
		panel.add(img);
		VerticalPanel vPanel = new VerticalPanel();
		vPanel.addStyleName(SWMMobile.getTheme().getMGWTCssBundle().getDecoratedListItemCss().decoratedListItemVPanel());
		titleLabel = new Label();
		titleLabel.addStyleName(SWMMobile.getTheme().getMGWTCssBundle().getDecoratedListItemCss()
				.decoratedListItemTitle());
		vPanel.add(titleLabel);
		subtitleLabel = new Label();
		subtitleLabel.addStyleName(SWMMobile.getTheme().getMGWTCssBundle().getDecoratedListItemCss()
				.decoratedListItemSubtitle());
		vPanel.add(subtitleLabel);
		panel.add(vPanel);
		add(panel);
	}



	/**
	 * Default constructor.
	 * 
	 * @param image
	 *            image od the text box
	 * @param title
	 *            title
	 * @param subtitle
	 *            subtitle
	 */
	@UiConstructor
	public DecoratedListItem(ImageResource image, String title, String subtitle) {
		this();
		img.setUrlAndVisibleRect(image.getSafeUri(), image.getLeft(), image.getTop(), image.getWidth(),
				image.getHeight());
		titleLabel.setText(title);
		subtitleLabel.setText(subtitle);
	}



	/**
	 * Sets an image.
	 * 
	 * @param image
	 *            the image
	 */
	public void setImage(ImageResource image) {
		img.setResource(image);
	}



	/**
	 * Sets an title.
	 * 
	 * @param title
	 *            the title
	 */
	public void setTitle(String title) {
		titleLabel.setText(title);
	}



	@Override
	public String getTitle() {
		return titleLabel.getText();
	}



	/**
	 * Sets the subtitle.
	 * 
	 * @param subtitle
	 *            subtitle
	 */
	public void setSubtitle(String subtitle) {
		subtitleLabel.setText(subtitle);
	}



	public String getSubtitle() {
		return subtitleLabel.getText();
	}



	/**
	 * Register a handler which processes clicks on the embedded image.
	 * 
	 * @param handler
	 *            the handler
	 * @return the handle registration
	 */
	public HandlerRegistration addImageClickHandler(ClickHandler handler) {
		return img.addClickHandler(handler);
	}

}
